package com.example.archermind.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OnlineActivity extends BaseActivity {

    @BindView(R.id.tv_isOnlineServiceOn)
    TextView tvIsOnlineServiceOn;
    @BindView(R.id.btn_isOnlineServiceOn)
    Button btnIsOnlineServiceOn;
    @BindView(R.id.tv_getManufacturer)
    TextView tvGetManufacturer;
    @BindView(R.id.btn_getManufacturer)
    Button btnGetManufacturer;
    @BindView(R.id.tv_registerOnLineCallback)
    TextView tvRegisterOnLineCallback;
    @BindView(R.id.btn_registerOnLineCallback)
    Button btnRegisterOnLineCallback;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String brand = bundle.getString("brand","");
            boolean isonline = bundle.getBoolean("isonline",false);
            int type = bundle.getInt("type",0);
            int variant = bundle.getInt("variant",0);
            switch (msg.what) {
                case CNSONLINECALLBACK + 0:
                    tvRegisterOnLineCallback.setText("onBrandChanged brand" + brand + "");
                    break;
                case CNSONLINECALLBACK + 1:
                    tvRegisterOnLineCallback.setText("onOnlineServiceChanged isonline:" + isonline + "");
                    break;
                case CNSONLINECALLBACK + 2:
                    tvRegisterOnLineCallback.setText("onManufacturerChanged type:" + type + "");
                    break;
                case CNSONLINECALLBACK + 3:
                    tvRegisterOnLineCallback.setText("onVariantChanged variant:"+ variant + "");
                    break;

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online);
        setHandler(mHandler);
        unbinder = ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_isOnlineServiceOn, R.id.btn_getManufacturer,R.id.btn_registerOnLineCallback})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_isOnlineServiceOn:
                try {
                    boolean isonline = getCnsOnLineService().isOnlineServiceOn();
                    tvIsOnlineServiceOn.setText("isOnlineServiceOn:"+isonline);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_getManufacturer:
                try {
                    int type = getCnsOnLineService().getManufacturer();
                    tvGetManufacturer.setText("getManufacturer:"+type);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_registerOnLineCallback:
                try {
                    getCnsOnLineService().registerOnLineCallback(getCnsOnLineCallback());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
